package com.fhc.view

import android.content.Intent
import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Usb
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.MaterialTheme.colorScheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri


@Preview(showBackground = true)
@Composable
fun LibraryAttribution() {
    Column(modifier = Modifier.padding(16.dp)) {
        Spacer(modifier = Modifier.height(16.dp))


        LibraryItem(
            name = "usb-serial-for-android",
            description = "This is a driver library for communication with Arduinos and other USB serial hardware on Android, ",
            license = "MIT License",
            link = "https://github.com/mik3y/usb-serial-for-android",
            imageVector = Icons.Default.Usb
        )
        Spacer(modifier = Modifier.height(12.dp))

        LibraryItem(
            name = "Coil",
            description = "An image loading library for Android and Compose Multiplatform",
            license = "Apache License",
            link = "https://github.com/coil-kt/coil",
            icon = com.fhc.view.R.drawable.coil
        )
        Spacer(modifier = Modifier.height(12.dp))

        LibraryItem(
            name = "Moshi",
            description = "A modern JSON library for Kotlin and Java.",
            license = "Apache License",
            link = "https://github.com/square/moshi",
            icon = com.fhc.view.R.drawable.moshi
        )
        Spacer(modifier = Modifier.height(12.dp))

        LibraryItem(
            name = "Sheets-Compose-Dialogs",
            description = "An Android library that offers dialogs & views for various use cases - build with Jetpack Compose.",
            license = "MIT License",
            link = "https://github.com/maxkeppeler/sheets-compose-dialogs",
            icon = com.fhc.view.R.drawable.sheets_compose_dialogs
        )

        Spacer(modifier = Modifier.height(12.dp))

        LibraryItem(
            name = "Android Snacky",
            description = "Android library for enhancing the snackbar experience with Jetpack Compose. ",
            license = "MIT License",
            link = "https://github.com/infinum/android-snacky",
            icon = com.fhc.view.R.drawable.snaky
        )

        Spacer(modifier = Modifier.height(12.dp))

        LibraryItem(
            name = "Toasty",
            description ="The usual Toast, but with steroids.",
            license = "GNU LGPL",
            link = "https://github.com/GrenderG/Toasty",
            icon = com.fhc.view.R.drawable.github
        )

        Spacer(modifier = Modifier.height(12.dp))

        LibraryItem(
            name = "Timber",
            description ="A logger with a small, extensible API which provides utility on top of Android's normal Log class.",
            license = "Apache License",
            link = "https://github.com/JakeWharton/timber",
            icon = com.fhc.view.R.drawable.timber
        )

        Spacer(modifier = Modifier.height(12.dp))

        LibraryItem(
            name = "Shimmer",
            description ="A simple shimmer library for Jetpack Compose.",
            license = " Apache License",
            link = "https://github.com/valentinilk/compose-shimmer",
            icon = com.fhc.view.R.drawable.github
        )
    }
}

@Composable
fun LibraryItem(name: String, description: String, license: String, link: String, @DrawableRes icon: Int? = null, imageVector: ImageVector? = null) {
    val context = LocalContext.current
    Card(
        modifier = Modifier.fillMaxWidth(),
        colors = CardDefaults.cardColors(
            containerColor = colorScheme.surfaceVariant,
            contentColor = colorScheme.onSurfaceVariant
        ),
        shape = MaterialTheme.shapes.medium
    ) {
        Column(modifier = Modifier.padding(16.dp)) {
            Row(verticalAlignment = Alignment.CenterVertically) {
                if(imageVector != null) {
                    Icon(
                        imageVector = imageVector,
                        contentDescription = null,
                        modifier = Modifier.size(24.dp)
                    )
                }
                if(icon != null) {
                    Image(
                        painter = painterResource(id = icon),
                        contentDescription = null,
                        modifier = Modifier.size(24.dp)
                    )
                }
                Spacer(modifier = Modifier.width(12.dp))
                Text(
                    text = name,
                    style = MaterialTheme.typography.titleMedium
                )
            }

            Spacer(modifier = Modifier.height(8.dp))
            Text(
                text = description,
                style = MaterialTheme.typography.bodyMedium
            )

            Spacer(modifier = Modifier.height(8.dp))
            Text(
                modifier = Modifier.fillMaxWidth(),
                text = license,
                style = MaterialTheme.typography.labelSmall,
                textAlign = TextAlign.End
            )

            Spacer(modifier = Modifier.height(12.dp))
            FilledTonalButton(
                onClick = {
                    val intent = Intent(Intent.ACTION_VIEW, link.toUri())
                    context.startActivity(intent)
                },
                modifier = Modifier.fillMaxWidth(),
                colors  = ButtonDefaults.filledTonalButtonColors(containerColor = colorScheme.onSurfaceVariant.copy(alpha = 0.3f))
            ) {
                Text(text = "Visit To")
            }
        }
    }
}
